/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.worlds.object;

import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import net.worlds.WorldsException;
import net.worlds.Main;
import net.worlds.PropChangeHandler;
import org.jbox2d.collision.shapes.CircleDef;
import org.jbox2d.common.Vec2D;


public class Circle extends Part{
    public Circle() throws WorldsException
    {
        super("Circle");
        addPropChangeHandler("Radius", new PropChangeHandler()
        {

            @Override
            public void handle(final Object oldValue, final Object value) throws WorldsException {
                float r = (Float) value;
                ((CircleDef)sd).radius = (float) r;
                    if(r < 1.5f)
                    {
                        if(inWorld)
                            body.setBullet(true);
                        else
                            bd.isBullet = true;
                    }
                if(inWorld)
                    rebuildShape();
            }

        });
        sd = new CircleDef();
        ((CircleDef)sd).radius = 2.5f;
        sd.restitution = 0.4f;
        sd.density = 1;
        sd.friction = 0.8f;
        bd.position = new Vec2D(0, 0);
        bd.angularDamping = 0.5f;
        if(Main.getGame().type != 0)
        {
            bd.linearDamping = 1f;
            bd.angularDamping = 1f;
        }
    }
    @Override
    public void draw(Graphics g) throws WorldsException
    {
        if(body == null || disabled)
            return;

        Graphics2D g2 = (Graphics2D) g;

        Vec2D worldPos = getDrawPosition();
        Vec2D screenVec = worldPos.worldToScreen(Main.getView());
        Vec2D size = new Vec2D((float) getFloatProp("Radius") * 2, (float) getFloatProp("Radius") * 2);
        Vec2D screenSize = size.toScreenUnits();
        double angle = -body.getAngle() - rotationDiff;
        int radius = (int) (getFloatProp("Radius") * Main.getView().scaleFactor);
        if(texture == null || showSelf)
        {
           Vec2D lineVec = new Vec2D(angle).toScreenUnits().mul((float) getFloatProp("Radius"));
            Vec2D lineEnd = new Vec2D((int) screenVec.x, (int) screenVec.y);
            lineEnd = lineEnd.add(lineVec);

            Color c = getColorProp("Color");
            g.setColor(new Color(c.getRed(), c.getGreen(), c.getBlue(), (int) ((1 - getFloatProp("Transparency"))*255)));
            g.fillOval((int) screenVec.x - radius, (int) screenVec.y - radius, radius * 2, radius * 2);
            c = c.darker();
            g.setColor(new Color(c.getRed(), c.getGreen(), c.getBlue(), (int) ((1 - getFloatProp("Transparency"))*255)));
            if(getBooleanProp("ShowBorder"))
                g.drawLine((int) screenVec.x, (int) screenVec.y, (int) lineEnd.x, (int) lineEnd.y);
        }
        else
        {
            Shape clip = g2.getClip();
            Vec2D imgPos = new Vec2D(worldPos.x - size.x * 0.5f, worldPos.y + size.y * 0.5f).worldToScreen(Main.getView());
            Shape clipShape = new Ellipse2D.Double(imgPos.x, imgPos.y, screenSize.x, screenSize.y);
            g2.setClip(clipShape);
            int ix = (int) Math.round(imgPos.x);
            int iy = (int) Math.round(imgPos.y);
            int ssx = (int) Math.round(screenSize.x);
            int ssy = (int) Math.round(screenSize.y);
            AffineTransform original = g2.getTransform();
            if(angle != 0.0)
            {
                AffineTransform rotate = AffineTransform.getRotateInstance(angle, screenVec.x, screenVec.y);
                g2.transform(rotate);
            }
            g2.drawImage(texture, ix, iy, ssx, ssy, null);
            g2.setTransform(original);
            g2.setClip(clip);
        }
        if(selected == true)
            g.setColor(Color.white);
        if((texture == null && getBooleanProp("ShowBorder") )|| selected)
            g.drawOval((int) screenVec.x - radius, (int) screenVec.y - radius, radius * 2, radius * 2);
        if(label != null)
        {
            drawLabel(g, null);
        }
    }
    @Override
    public void drawLabel(Graphics g, Vec2D pos) throws WorldsException
    {
        if(label == null)
            return;
        Vec2D center = getPosition().worldToScreen(Main.getView());
        FontMetrics fm = g.getFontMetrics();
        Rectangle2D r = fm.getStringBounds(label, g);
        int textX = (int) (center.x - r.getWidth()/2);
        int textY = (int) (center.y - getFloatProp("Radius") * Main.getView().scaleFactor - 10);
        g.setColor(Color.black);
        g.drawString(label, textX + 1, textY + 1);
        g.setColor(Color.white);
        g.drawString(label, textX, textY);
    }
}
